package io.mft.web.adapter.api.response;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import io.mft.commons.function.response.DsName;
import io.mft.runtime.function.ApiFunctionResponse;

public class DataWebApiResponse extends WebApiResponse {

	/** 状态码 */
	public static final String CODE_TAG = "code";
	/** 返回内容 */
	public static final String MSG_TAG = "msg";
	/** 数据对象 */
	public static final String DATA_TAG = "data";
	public static final String DEFAULT_SUCCESS_CODE = "00000";
	public static final String DEFAULT_SUCCESS_MSG = "SUCCESS";
	/**
	 * @说明：TODO
	 * @字段名称 serialVersionUID
	 */

	private static final long serialVersionUID = -8614787127512021008L;

	public DataWebApiResponse(ApiFunctionResponse response) {
		super(response);
		init(response);
	}
	
	private void init(ApiFunctionResponse response) {
		List<DsName> dsNames = response.getDsName();
		if (response.getErrorNo() == 0) {
			for (DsName dsName : dsNames) {
				switch (dsName.getType()) {
				case list:
					if (dsName.getName().equals(dsName.getMainOutputDsKey())) {
						if (ObjectUtil.isNotNull(response.listObject(dsName.getName()))) {
							data(response.listObject(dsName.getName()));
						}
						
					} else {
						if (ObjectUtil.isNotNull(response.listObject(dsName.getName()))) {
							data(dsName.getName(), response.listObject(dsName.getName()));
						}
					}
					break;
				case map:
					if (dsName.getName().equals(dsName.getMainOutputDsKey())) {
						if (ObjectUtil.isNotNull(response.dataObject(dsName.getName()))) {
							data(response.dataObject(dsName.getName()));
						}
					} else {
						if (ObjectUtil.isNotNull(response.dataObject(dsName.getName()))) {
							data(dsName.getName(), response.dataObject(dsName.getName()));
						}
					}
					break;
				case page:
					if (dsName.getName().equals(dsName.getMainOutputDsKey())) {
						if (ObjectUtil.isNotNull(response.pageTObject(dsName.getName()))) {
							data(response.pageTObject(dsName.getName()));
						}
					} else {
						if (ObjectUtil.isNotNull(response.pageTObject(dsName.getName()))) {
							data(dsName.getName(), response.pageTObject(dsName.getName()));
						}
					}
					break;
				default:
					break;
				}
			}
		}		
	}
	public DataWebApiResponse(){
		super();
	}
	@Override
	public void setErrorNo(int errorNo) {
		put(CODE_TAG, errorNo);
	}

	@Override
	public void setErrorInfo(String errorInfo) {
		put(MSG_TAG, errorInfo);
	}

	@Override
	public void data(Object value) {
		data(DATA_TAG, value);
	}
}
